#include "../src/biTree/biTree.h"
#include <stdio.h>
#include <stdlib.h>

extern TFBiTree fBiTree;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/****
 * 指路法通过根节点也目标节点的相对位置进行定位
 * 指路法可以避开二叉树递归的性质进行“线性”的定位
 * 在C语言中可以通过bit位进行指路
 ****/
/****
 * 和单链表类似
 ****/
typedef struct __Node
{
       BiTreeNode header; /* 头指针 */
       char v;            /* 自定义的数据域 */
} Node;

void printf_data(BiTreeNode *node)
{
       if (node != NULL)
              printf("%c ", ((Node *)node)->v);
}

int main(int argc, char *argv[])
{
       BiTree *tree = fBiTree.create();

       Node n1, n2, n3, n4, n5, n6;

       n1.v = 'A';
       n1.header.lchild = NULL;
       n1.header.rchild = NULL;
       n2.v = 'B';
       n2.header.lchild = NULL;
       n2.header.rchild = NULL;
       n3.v = 'C';
       n3.header.lchild = NULL;
       n3.header.rchild = NULL;
       n4.v = 'D';
       n4.header.lchild = NULL;
       n4.header.rchild = NULL;
       n5.v = 'E';
       n5.header.lchild = NULL;
       n5.header.rchild = NULL;
       n6.v = 'F';
       n6.header.lchild = NULL;
       n6.header.rchild = NULL;

       fBiTree.insert(tree, (BiTreeNode *)&n1, 0, 0);
       fBiTree.insert(tree, (BiTreeNode *)&n2, 0x00, 1);
       fBiTree.insert(tree, (BiTreeNode *)&n3, 0x01, 1);
       fBiTree.insert(tree, (BiTreeNode *)&n4, 0x00, 2);
       fBiTree.insert(tree, (BiTreeNode *)&n5, 0x02, 2);
       fBiTree.insert(tree, (BiTreeNode *)&n6, 0x02, 3);

       printf("Height = %d, Degree = %d, Count = %d\n",
              fBiTree.depth(tree), fBiTree.degree(tree), fBiTree.count(tree));

       printf("Position At (0x02, 2): %c\n", ((Node *)fBiTree.get(tree, 0x02, 2))->v);

       printf("Prev Older: ");
       fBiTree.display(tree, printf_data, 0);
       printf("in   Older: ");
       fBiTree.display(tree, printf_data, 1);
       printf("post Older: ");
       fBiTree.display(tree, printf_data, 2);
       printf("layerOlder: ");
       fBiTree.display(tree, printf_data, 3);

       printf("\nAfter Insert G: \n");
       Node n7;
       n7.v = 'G';
       n7.header.lchild = NULL;
       n7.header.rchild = NULL;
       fBiTree.insert(tree, (BiTreeNode *)&n7, 0x01, 1);
       printf("Height = %d, Degree = %d, Count = %d\n",
              fBiTree.depth(tree), fBiTree.degree(tree), fBiTree.count(tree));

       printf("Prev Older: ");
       fBiTree.display(tree, printf_data, 0);
       printf("in   Older: ");
       fBiTree.display(tree, printf_data, 1);
       printf("post Older: ");
       fBiTree.display(tree, printf_data, 2);
       printf("layerOlder: ");
       fBiTree.display(tree, printf_data, 3);

       Node n8;
       n8.v = 'H';
       n8.header.lchild = NULL;
       n8.header.rchild = NULL;

       printf("\nAfter Update B->H: \n");
       fBiTree.update(tree, 0x00, 1, (BiTreeNode *)&(n8));

       printf("Prev Older: ");
       fBiTree.display(tree, printf_data, 0);
       printf("in   Older: ");
       fBiTree.display(tree, printf_data, 1);
       printf("post Older: ");
       fBiTree.display(tree, printf_data, 2);
       printf("layerOlder: ");
       fBiTree.display(tree, printf_data, 3);

       fBiTree.delete(tree, 0x00, 1);

       printf("\nAfter Delete B: \n");
       printf("Height = %d, Degree = %d, Count = %d\n",
              fBiTree.depth(tree), fBiTree.degree(tree), fBiTree.count(tree));

       printf("Prev Older: ");
       fBiTree.display(tree, printf_data, 0);
       printf("in   Older: ");
       fBiTree.display(tree, printf_data, 1);
       printf("post Older: ");
       fBiTree.display(tree, printf_data, 2);
       printf("layerOlder: ");
       fBiTree.display(tree, printf_data, 3);

       fBiTree.clear(tree);

       printf("\nAfter Clear: ");
       printf("Height = %d, Degree = %d, Count = %d\n",
              fBiTree.depth(tree), fBiTree.degree(tree), fBiTree.count(tree));

       fBiTree.destroy(tree);

       return 0;
}
